package com.jason.www.jdk8;


import com.jason.www.jdk8.pojo.Person;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;

/**
 * Stream API的中间操作
 */
public class D_StreamAPI_2 {
    List<Person> persons = Arrays.asList(
            new Person(2, "钱四", 24),
            new Person(1, "张三", 33),
            new Person(2, "李四", 24),
            new Person(3, "王五", 65),
            new Person(4, "赵六", 26),
            new Person(4, "赵六", 26),
            new Person(5, "陈七", 27)
    );

    //内部迭代，由Stream API完成
    @Test
    public void test1(){
        //中间操作,不会执行任何操作
        Stream<Person> stream = persons.stream()
                .filter((e) -> {
                    System.out.println("Stream的中间操作");
                    return e.getAge() > 25;
                });
        //终止操作，一次性执行全部内容，即"惰性求值"
        stream.forEach(System.out :: println);
    }

    //外部迭代
    @Test
    public void test2(){
        Iterator<Person> iterator = persons.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().getName());
        }
    }

    //limit，截断
    @Test
    public void test3(){
        persons.stream()
                .filter((e) -> {
                    System.out.println("迭代操作"); //短路
                    return e.getAge() > 24;
                })
                .limit(2)
                .forEach(System.out :: println);
    }

    //跳过skip,distinct去重(要重写equals和hashcode)
    @Test
    public void test4(){
        persons.stream()
                .filter((e) -> e.getAge() > 23)
                .skip(2)
                .distinct()
                .forEach(System.out :: println);
    }

    //映射
    @Test
    public void test5(){
        List<String> list = Arrays.asList("a","bb","c","d","e");
        for (String s : list) {
            System.out.println(s.toUpperCase());
        }
        list.stream().map((str) -> str.toUpperCase())
                .forEach(System.out :: println);

        System.out.println("---------------");

        persons.stream().map((Person :: getName)).forEach(System.out :: println);
        System.out.println("---------------");

        Stream<Stream<Character>> stream = list.stream()
                .map(D_StreamAPI_2 :: filterCharacter);

        stream.forEach((s) -> {
            s.forEach(System.out :: println);
        });
        System.out.println("-----------------");

        //flatMap
        Stream<Character> stream2 = list.stream()
                .flatMap(D_StreamAPI_2 :: filterCharacter);
        stream2.forEach(System.out :: println);
    }

    //处理字符串
    public static Stream<Character> filterCharacter(String str){
        List<Character> list = new ArrayList<>();

        for (Character character : str.toCharArray()) {
            list.add(character);
        }
        return list.stream();
    }

    //排序
    @Test
    public void test6(){
        List<String> list = Arrays.asList("bb","c","aa","ee","ddd");

        list.stream()
                .sorted() //自然排序
                .forEach(System.out :: println);
        System.out.println("------------");

        persons.stream()
                .sorted((p1,p2) -> {//定制排序
                    if (p1.getAge() == p2.getAge()) {
                        return p1.getName().compareTo(p2.getName());
                    } else {
                        return p1.getAge() - p2.getAge();
                    }
                }).forEach(System.out :: println);

    }
}
